package com.coffee.socketio.command;

import cn.hutool.core.util.StrUtil;
import com.coffee.socketio.metadata.ServerParameter;
import lombok.extern.slf4j.Slf4j;

import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: songkui
 * @since: 2023/2/22 16:24
 */
@Slf4j
public class CommandModelMapping {

    private static final ConcurrentHashMap<String, CommandModel> commandSocketModelMap = new ConcurrentHashMap();
    private final static Set<ServerParameter> serverParamSet = new HashSet<>(); // socket server param


    private CommandModelMapping() {
    }

    public static CommandModel getSocketCommandModel(String k) {
        if (commandSocketModelMap.isEmpty()) {
            log.error("CMD -> commandSocketModelMap  is null !");
            return null;
        } else {
            return commandSocketModelMap.get(k);
        }
    }

    public static void initializeCommandModel(Map<Integer, CommandModel> commandMap) throws Exception {
        if (null != commandMap) {
            commandMap.forEach ((k,v) -> {
                if (StrUtil.isNotBlank(v.getEvent())){
                    commandSocketModelMap.put(Optional.ofNullable(v.getNameSpace()).orElse("") + v.getEvent(), v);
                }
            });

        }
        log.info("commandSocketModelMap size: {}", commandSocketModelMap.size());
    }


    public static Set<ServerParameter> getServerParamSet(){
        return serverParamSet;
    }

    public static void addServerParam(ServerParameter parameter){
        serverParamSet.add(parameter);
    }


}
